import http from '@/utils/http';
import type { 
  AiChatRecord, 
  AiChatQueryParams, 
  AiChatStatistics,
  AiChatApiResponse,
  BatchDeleteRequest
} from '@/types';

/**
 * 获取AI对话记录列表（支持搜索和分页）
 */
export const getAiChatList = async (params: AiChatQueryParams): Promise<AiChatApiResponse<AiChatRecord>> => {
  const response = await http.get('/admin/ai-chat', { params });
  return response.data;
};

/**
 * 获取AI对话记录详情
 */
export const getAiChatDetail = async (id: number): Promise<AiChatRecord> => {
  const response = await http.get(`/admin/ai-chat/${id}`);
  return response.data.data;
};

/**
 * 获取某个会话的所有对话记录
 */
export const getSessionChatRecords = async (sessionId: string, sort?: string): Promise<AiChatRecord[]> => {
  const response = await http.get(`/admin/ai-chat/session/${sessionId}`, {
    params: { sort }
  });
  return response.data.data;
};

/**
 * 删除单条AI对话记录
 */
export const deleteAiChatRecord = async (id: number): Promise<void> => {
  await http.delete(`/admin/ai-chat/${id}`);
};

/**
 * 批量删除AI对话记录
 */
export const batchDeleteAiChatRecords = async (ids: number[]): Promise<{
  deletedCount: number;
}> => {
  const response = await http.delete('/admin/ai-chat/batch', { data: { ids } });
  return response.data.data;
};

/**
 * 删除某个会话的所有对话记录
 */
export const deleteSessionChatRecords = async (sessionId: string): Promise<{
  deletedCount: number;
}> => {
  const response = await http.delete(`/admin/ai-chat/session/${sessionId}`);
  return response.data.data;
};

/**
 * 删除某个用户的所有AI对话记录
 */
export const deleteUserAiChatRecords = async (userId: number): Promise<{
  deletedCount: number;
}> => {
  const response = await http.delete(`/admin/ai-chat/user/${userId}`);
  return response.data.data;
};

/**
 * 清理过期的AI对话记录
 */
export const cleanupExpiredChatRecords = async (days: number = 30): Promise<{
  deletedCount: number;
  cleanupDays: number;
}> => {
  const response = await http.delete('/admin/ai-chat/cleanup/expired', {
    params: { days }
  });
  return response.data.data;
};

/**
 * 获取AI对话统计信息
 */
export const getAiChatStatistics = async (): Promise<AiChatStatistics> => {
  const response = await http.get('/admin/ai-chat/stats/overview');
  return response.data.data;
}; 